package com.example.dict.ativities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;

import com.example.dict.R;

import java.util.ArrayList;
import java.util.List;

public class QueryChengYuActivity extends AppCompatActivity {

    EditText mSearchEt;
    GridView mChyuGv;
    List<String> mDatas;
    private ArrayAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_cheng_yu);
        mSearchEt = findViewById(R.id.searchchyu_et);
        mChyuGv = findViewById(R.id.searchchyu_gv);
        mDatas = new ArrayList<>();
        //设置适配器，因为只有一个子 textView,所以用以下这种方式设置，R.layout是子布局，R.id是子布局里面的textView点id,mDatas是数据
        mAdapter = new ArrayAdapter<>(this, R.layout.item_searchchyu_gv, R.id.item_searchchyu_tv, mDatas);
        mChyuGv.setAdapter(mAdapter);
        //设置GridView的点击事件
        setChyuGvListener();
    }

    /**
     * GridView 每一个item点击事件
     */
    private void setChyuGvListener() {
        mChyuGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String msg = mDatas.get(position);//获取数据源
                startPage(msg);

            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.searchyu_iv_back:
                finish();
                break;
            case R.id.ic_search:
                String text = mSearchEt.getText().toString();
                if (TextUtils.isEmpty(text)) {
                    return;
                }
                //跳转到成语详情页面
                startPage(text);
                break;
        }
    }

    /**
     * 携带成语跳转到下一个页面
     * @param text
     */
    private void startPage(String text) {
        Intent intent = new Intent(this, ChengyuInfoActivity.class);
        intent.putExtra("chengyu",text);
        startActivity(intent);
    }
}
